
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>通用日期视图 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="基于类的视图混入" href="mixins.html" />
    <link rel="prev" title="通用编辑视图" href="generic-editing.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="generic-editing.html" title="通用编辑视图">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="mixins.html" title="基于类的视图混入">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-class-based-views-generic-date-based">
            
  <div class="section" id="s-module-django.views.generic.dates">
<span id="s-generic-date-views"></span><span id="module-django.views.generic.dates"></span><span id="generic-date-views"></span><h1>通用日期视图<a class="headerlink" href="#module-django.views.generic.dates" title="永久链接至标题">¶</a></h1>
<p>基于日期的通用视图，提供在 <a class="reference internal" href="#module-django.views.generic.dates" title="django.views.generic.dates"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.views.generic.dates</span></code></a> 中，是用于显示基于日期数据的向下扩展页面的视图。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>本页的一些例子假定 <code class="docutils literal notranslate"><span class="pre">Article</span></code> 模型已在 <code class="docutils literal notranslate"><span class="pre">myapp/models.py</span></code> 中定义如下：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">reverse</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
    <span class="n">pub_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">get_absolute_url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">reverse</span><span class="p">(</span><span class="s1">&#39;article-detail&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;pk&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">pk</span><span class="p">})</span>
</pre></div>
</div>
</div>
<div class="section" id="s-archiveindexview">
<span id="archiveindexview"></span><h2><code class="docutils literal notranslate"><span class="pre">ArchiveIndexView</span></code><a class="headerlink" href="#archiveindexview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.ArchiveIndexView">
<em class="property">class </em><code class="descname">ArchiveIndexView</code><a class="headerlink" href="#django.views.generic.dates.ArchiveIndexView" title="永久链接至目标">¶</a></dt>
<dd><p>一个按日期显示“最新”对象的顶层索引页。除非你将 <code class="docutils literal notranslate"><span class="pre">allow_future</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则不包含日期在 <em>未来</em> 的对象。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseArchiveIndexView" title="django.views.generic.dates.BaseArchiveIndexView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseArchiveIndexView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a></li>
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>上下文</strong></p>
<p>除了 <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a> （通过 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a>）提供的上下文外，模板的上下文将是：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">date_list</span></code>：一个 <a class="reference internal" href="../models/querysets.html#django.db.models.query.QuerySet.dates" title="django.db.models.query.QuerySet.dates"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 对象，包含根据 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 有对象可用的所有年份，以 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> 对象表示，按降序排列。</li>
</ul>
<p><strong>注意</strong></p>
<ul class="simple">
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">latest</span></code> 的 <code class="docutils literal notranslate"><span class="pre">context_object_name</span></code>。</li>
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">_archive</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</li>
<li>默认情况下，按年提供 <code class="docutils literal notranslate"><span class="pre">date_list</span></code>，但可以使用 <code class="docutils literal notranslate"><span class="pre">date_list_period</span></code> 属性将其改为月或日。这也适用于所有子类视图。</li>
</ul>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">ArchiveIndexView</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;archive/&#39;</span><span class="p">,</span>
         <span class="n">ArchiveIndexView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="n">Article</span><span class="p">,</span> <span class="n">date_field</span><span class="o">=</span><span class="s2">&quot;pub_date&quot;</span><span class="p">),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;article_archive&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>示例 myapp/article_archive.html</strong>：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">article</span> <span class="k">in</span> <span class="nv">latest</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">article.pub_date</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">article.title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>这将输出所有文章。</p>
</dd></dl>

</div>
<div class="section" id="s-yeararchiveview">
<span id="yeararchiveview"></span><h2><code class="docutils literal notranslate"><span class="pre">YearArchiveView</span></code><a class="headerlink" href="#yeararchiveview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.YearArchiveView">
<em class="property">class </em><code class="descname">YearArchiveView</code><a class="headerlink" href="#django.views.generic.dates.YearArchiveView" title="永久链接至目标">¶</a></dt>
<dd><p>每年的存档页，显示某一年的所有可用月份。除非你把 <code class="docutils literal notranslate"><span class="pre">allow_future</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则不显示日期在 <em>未来</em> 的对象。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseYearArchiveView" title="django.views.generic.dates.BaseYearArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseYearArchiveView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.YearMixin" title="django.views.generic.dates.YearMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.YearMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a></li>
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<dl class="attribute">
<dt id="django.views.generic.dates.YearArchiveView.make_object_list">
<code class="descname">make_object_list</code><a class="headerlink" href="#django.views.generic.dates.YearArchiveView.make_object_list" title="永久链接至目标">¶</a></dt>
<dd><p>一个布尔值，用于指定是否检索今年的全部对象清单并将其传递给模板。如果 <code class="docutils literal notranslate"><span class="pre">True</span></code>，将向上下文提供对象清单。如果 <code class="docutils literal notranslate"><span class="pre">False</span></code>，将使用 <code class="docutils literal notranslate"><span class="pre">None</span></code> 查询集作为对象列表。默认情况下，这是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="method">
<dt id="django.views.generic.dates.YearArchiveView.get_make_object_list">
<code class="descname">get_make_object_list</code>()<a class="headerlink" href="#django.views.generic.dates.YearArchiveView.get_make_object_list" title="永久链接至目标">¶</a></dt>
<dd><p>确定是否将对象列表作为上下文的一部分返回。默认返回 <a class="reference internal" href="#django.views.generic.dates.YearArchiveView.make_object_list" title="django.views.generic.dates.YearArchiveView.make_object_list"><code class="xref py py-attr docutils literal notranslate"><span class="pre">make_object_list</span></code></a>。</p>
</dd></dl>

<p><strong>上下文</strong></p>
<p>除了 <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a> （通过 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a>）提供的上下文外，模板的上下文将是：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">date_list</span></code>。一个 <a class="reference internal" href="../models/querysets.html#django.db.models.query.QuerySet.dates" title="django.db.models.query.QuerySet.dates"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 对象，包含根据 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 有对象可用的所有月份，以 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> 对象表示，按升序排列。</li>
<li><code class="docutils literal notranslate"><span class="pre">year</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，代表特定年份。</li>
<li><code class="docutils literal notranslate"><span class="pre">next_year</span></code>： 一个:class:<cite>~datetime.date</cite> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示下一年的第一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">previous_year</span></code>： 一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示前一年的第一天。</li>
</ul>
<p><strong>注意</strong></p>
<ul class="simple">
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">_archive_year</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</li>
</ul>
<p><strong>示例 myapp/views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">YearArchiveView</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">ArticleYearArchiveView</span><span class="p">(</span><span class="n">YearArchiveView</span><span class="p">):</span>
    <span class="n">queryset</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
    <span class="n">date_field</span> <span class="o">=</span> <span class="s2">&quot;pub_date&quot;</span>
    <span class="n">make_object_list</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">ArticleYearArchiveView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;int:year&gt;/&#39;</span><span class="p">,</span>
         <span class="n">ArticleYearArchiveView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;article_year_archive&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>示例 myapp/article_archive_year.html</strong>：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">date_list</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>All Articles for <span class="cp">{{</span> <span class="nv">year</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y&quot;</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">obj</span> <span class="k">in</span> <span class="nv">object_list</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
            <span class="cp">{{</span> <span class="nv">obj.title</span> <span class="cp">}}</span> - <span class="cp">{{</span> <span class="nv">obj.pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F j, Y&quot;</span> <span class="cp">}}</span>
        <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-montharchiveview">
<span id="montharchiveview"></span><h2><code class="docutils literal notranslate"><span class="pre">MonthArchiveView</span></code><a class="headerlink" href="#montharchiveview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.MonthArchiveView">
<em class="property">class </em><code class="descname">MonthArchiveView</code><a class="headerlink" href="#django.views.generic.dates.MonthArchiveView" title="永久链接至目标">¶</a></dt>
<dd><p>每月的存档页，显示特定月份的所有对象。除非你把 <code class="docutils literal notranslate"><span class="pre">allow_future</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则不显示日期在 <em>未来</em> 的对象。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseMonthArchiveView" title="django.views.generic.dates.BaseMonthArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseMonthArchiveView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.YearMixin" title="django.views.generic.dates.YearMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.YearMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.MonthMixin" title="django.views.generic.dates.MonthMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.MonthMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a></li>
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>上下文</strong></p>
<p>除了 <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultipleObjectMixin</span></code></a> （通过 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseDateListView</span></code></a>）提供的上下文外，模板的上下文将是：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">date_list</span></code>：一个 <a class="reference internal" href="../models/querysets.html#django.db.models.query.QuerySet.dates" title="django.db.models.query.QuerySet.dates"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet</span></code></a> 对象，根据 <code class="docutils literal notranslate"><span class="pre">queryset</span></code>，以 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> 对象的形式表示，按升序排列，包含给定月份内有对象可用的所有日子。</li>
<li><code class="docutils literal notranslate"><span class="pre">month</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，代表指定月份。</li>
<li><code class="docutils literal notranslate"><span class="pre">next_month</span></code>： 一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示下个月的第一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">previous_month</span></code>： 一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示前一个月的第一天。</li>
</ul>
<p><strong>注意</strong></p>
<ul class="simple">
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">_archive_month</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</li>
</ul>
<p><strong>示例 myapp/views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">MonthArchiveView</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">ArticleMonthArchiveView</span><span class="p">(</span><span class="n">MonthArchiveView</span><span class="p">):</span>
    <span class="n">queryset</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
    <span class="n">date_field</span> <span class="o">=</span> <span class="s2">&quot;pub_date&quot;</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">ArticleMonthArchiveView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># Example: /2012/08/</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;int:year&gt;/&lt;int:month&gt;/&#39;</span><span class="p">,</span>
         <span class="n">ArticleMonthArchiveView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">month_format</span><span class="o">=</span><span class="s1">&#39;%m&#39;</span><span class="p">),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;archive_month_numeric&quot;</span><span class="p">),</span>
    <span class="c1"># Example: /2012/aug/</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;int:year&gt;/&lt;str:month&gt;/&#39;</span><span class="p">,</span>
         <span class="n">ArticleMonthArchiveView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;archive_month&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>示例 myapp/article_archive_month.html</strong>：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">article</span> <span class="k">in</span> <span class="nv">object_list</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">article.pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F j, Y&quot;</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">article.title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">previous_month</span> <span class="cp">%}</span>
        Previous Month: <span class="cp">{{</span> <span class="nv">previous_month</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F Y&quot;</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">next_month</span> <span class="cp">%}</span>
        Next Month: <span class="cp">{{</span> <span class="nv">next_month</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F Y&quot;</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-weekarchiveview">
<span id="weekarchiveview"></span><h2><code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code><a class="headerlink" href="#weekarchiveview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.WeekArchiveView">
<em class="property">class </em><code class="descname">WeekArchiveView</code><a class="headerlink" href="#django.views.generic.dates.WeekArchiveView" title="永久链接至目标">¶</a></dt>
<dd><p>每周的档案页，显示某一周的所有对象。除非你将 <code class="docutils literal notranslate"><span class="pre">allow_future</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则不显示日期在 <em>未来</em> 的对象。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseWeekArchiveView" title="django.views.generic.dates.BaseWeekArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseWeekArchiveView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.YearMixin" title="django.views.generic.dates.YearMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.YearMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.WeekMixin" title="django.views.generic.dates.WeekMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.WeekMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a></li>
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>上下文</strong></p>
<p>除了 <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultipleObjectMixin</span></code></a> （通过 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseDateListView</span></code></a>）提供的上下文外，模板的上下文将是：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">week</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，表示指定星期的第一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">next_week</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示下周的第一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">previous_week</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示前一周的第一天。</li>
</ul>
<p><strong>注意</strong></p>
<ul>
<li><p class="first">使用默认为 <code class="docutils literal notranslate"><span class="pre">_archive_week</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">week_format</span></code> 属性是一个 <a class="reference external" href="https://docs.python.org/3/library/time.html#time.strptime" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a> 格式字符串，用于解析星期数。支持的值如下：</p>
<ul>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">'%U'</span></code>：根据美国的周制，一周从周日开始。这是默认值。</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">'%W'</span></code>：与 <code class="docutils literal notranslate"><span class="pre">'%U'</span></code> 类似，但它假定一周从星期一开始。这与 ISO 8601 周号不同。</p>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">'%V'</span></code> ： ISO 8601 周号，即一周开始于星期一。</p>
<div class="versionadded">
<span class="title">New in Django 3.2:</span> <p>增加了对 <code class="docutils literal notranslate"><span class="pre">'%V'</span></code> 周格式的支持。</p>
</div>
</li>
</ul>
</li>
</ul>
<p><strong>示例 myapp/views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">WeekArchiveView</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">ArticleWeekArchiveView</span><span class="p">(</span><span class="n">WeekArchiveView</span><span class="p">):</span>
    <span class="n">queryset</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
    <span class="n">date_field</span> <span class="o">=</span> <span class="s2">&quot;pub_date&quot;</span>
    <span class="n">week_format</span> <span class="o">=</span> <span class="s2">&quot;%W&quot;</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">ArticleWeekArchiveView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># Example: /2012/week/23/</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;int:year&gt;/week/&lt;int:week&gt;/&#39;</span><span class="p">,</span>
         <span class="n">ArticleWeekArchiveView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;archive_week&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>示例 myapp/article_archive_week.html</strong>：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Week <span class="cp">{{</span> <span class="nv">week</span><span class="o">|</span><span class="nf">date</span><span class="s1">:&#39;W&#39;</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">article</span> <span class="k">in</span> <span class="nv">object_list</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">article.pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F j, Y&quot;</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">article.title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">previous_week</span> <span class="cp">%}</span>
        Previous Week: <span class="cp">{{</span> <span class="nv">previous_week</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;W&quot;</span> <span class="cp">}}</span> of year <span class="cp">{{</span> <span class="nv">previous_week</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y&quot;</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">previous_week</span> <span class="k">and</span> <span class="nv">next_week</span> <span class="cp">%}</span>--<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">next_week</span> <span class="cp">%}</span>
        Next week: <span class="cp">{{</span> <span class="nv">next_week</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;W&quot;</span> <span class="cp">}}</span> of year <span class="cp">{{</span> <span class="nv">next_week</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y&quot;</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>在这个例子中，你输出的是周数。请记住，由 <a class="reference internal" href="../templates/builtins.html#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 模板过滤器用 <cite>'`W'`</cite> 格式字符计算的星期数与 <a class="reference external" href="https://docs.python.org/3/library/time.html#time.strftime" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code></a> 和 <a class="reference external" href="https://docs.python.org/3/library/time.html#time.strptime" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">strptime()</span></code></a> 用 <code class="docutils literal notranslate"><span class="pre">'%W'</span></code> 格式字符串计算的星期数并不总是相同的。例如，对于 2015 年， <a class="reference internal" href="../templates/builtins.html#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 输出的周数比 <a class="reference external" href="https://docs.python.org/3/library/time.html#time.strftime" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code></a> 输出的周数高 1。<a class="reference internal" href="../templates/builtins.html#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 中的 <code class="docutils literal notranslate"><span class="pre">'%U'</span></code> <a class="reference external" href="https://docs.python.org/3/library/time.html#time.strftime" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">strftime()</span></code></a> 格式字符串没有等价物。因此，你应该避免使用 <a class="reference internal" href="../templates/builtins.html#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code> 生成 URL。</p>
</dd></dl>

</div>
<div class="section" id="s-dayarchiveview">
<span id="dayarchiveview"></span><h2><code class="docutils literal notranslate"><span class="pre">DayArchiveView</span></code><a class="headerlink" href="#dayarchiveview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.DayArchiveView">
<em class="property">class </em><code class="descname">DayArchiveView</code><a class="headerlink" href="#django.views.generic.dates.DayArchiveView" title="永久链接至目标">¶</a></dt>
<dd><p>一个显示某一天所有对象的日存档页面。未来的日子会抛出 404 错误，不管未来的日子是否有任何对象存在，除非你把 <code class="docutils literal notranslate"><span class="pre">allow_future</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseDayArchiveView" title="django.views.generic.dates.BaseDayArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDayArchiveView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.YearMixin" title="django.views.generic.dates.YearMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.YearMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.MonthMixin" title="django.views.generic.dates.MonthMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.MonthMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DayMixin" title="django.views.generic.dates.DayMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DayMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a></li>
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>上下文</strong></p>
<p>除了 <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultipleObjectMixin</span></code></a> （通过 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseDateListView</span></code></a>）提供的上下文外，模板的上下文将是：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">day</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，代表指定的一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">next_day</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a> 表示第二天。</li>
<li><code class="docutils literal notranslate"><span class="pre">previous_day</span></code>：一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a> 表示前一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">next_month</span></code>： 一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示下个月的第一天。</li>
<li><code class="docutils literal notranslate"><span class="pre">previous_month</span></code>： 一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.date" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code></a> 对象，根据 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView.allow_empty" title="django.views.generic.dates.BaseDateListView.allow_empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_empty</span></code></a> 和 <a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin.allow_future" title="django.views.generic.dates.DateMixin.allow_future"><code class="xref py py-attr docutils literal notranslate"><span class="pre">allow_future</span></code></a>，表示前一个月的第一天。</li>
</ul>
<p><strong>注意</strong></p>
<ul class="simple">
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">_archive_day</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</li>
</ul>
<p><strong>示例 myapp/views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">DayArchiveView</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">ArticleDayArchiveView</span><span class="p">(</span><span class="n">DayArchiveView</span><span class="p">):</span>
    <span class="n">queryset</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
    <span class="n">date_field</span> <span class="o">=</span> <span class="s2">&quot;pub_date&quot;</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">ArticleDayArchiveView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># Example: /2012/nov/10/</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;int:year&gt;/&lt;str:month&gt;/&lt;int:day&gt;/&#39;</span><span class="p">,</span>
         <span class="n">ArticleDayArchiveView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;archive_day&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>示例 myapp/article_archive_day.html</strong>：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">day</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">article</span> <span class="k">in</span> <span class="nv">object_list</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">article.pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;F j, Y&quot;</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">article.title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">previous_day</span> <span class="cp">%}</span>
        Previous Day: <span class="cp">{{</span> <span class="nv">previous_day</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">previous_day</span> <span class="k">and</span> <span class="nv">next_day</span> <span class="cp">%}</span>--<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">next_day</span> <span class="cp">%}</span>
        Next Day: <span class="cp">{{</span> <span class="nv">next_day</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-todayarchiveview">
<span id="todayarchiveview"></span><h2><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code><a class="headerlink" href="#todayarchiveview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.TodayArchiveView">
<em class="property">class </em><code class="descname">TodayArchiveView</code><a class="headerlink" href="#django.views.generic.dates.TodayArchiveView" title="永久链接至目标">¶</a></dt>
<dd><p>一个显示 <em>今天</em> 所有对象的日存档页面。这和 <a class="reference internal" href="#django.views.generic.dates.DayArchiveView" title="django.views.generic.dates.DayArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DayArchiveView</span></code></a> 完全一样，只是用今天的日期代替 <code class="docutils literal notranslate"><span class="pre">year</span></code>／<code class="docutils literal notranslate"><span class="pre">month</span></code>／<code class="docutils literal notranslate"><span class="pre">day</span></code> 参数。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseTodayArchiveView" title="django.views.generic.dates.BaseTodayArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseTodayArchiveView</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseDayArchiveView" title="django.views.generic.dates.BaseDayArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDayArchiveView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.YearMixin" title="django.views.generic.dates.YearMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.YearMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.MonthMixin" title="django.views.generic.dates.MonthMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.MonthMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DayMixin" title="django.views.generic.dates.DayMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DayMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.BaseDateListView" title="django.views.generic.dates.BaseDateListView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateListView</span></code></a></li>
<li><a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectMixin" title="django.views.generic.list.MultipleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.list.MultipleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>注意</strong></p>
<ul class="simple">
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">_archive_today</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</li>
</ul>
<p><strong>示例 myapp/views.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">TodayArchiveView</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Article</span>

<span class="k">class</span> <span class="nc">ArticleTodayArchiveView</span><span class="p">(</span><span class="n">TodayArchiveView</span><span class="p">):</span>
    <span class="n">queryset</span> <span class="o">=</span> <span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
    <span class="n">date_field</span> <span class="o">=</span> <span class="s2">&quot;pub_date&quot;</span>
    <span class="n">allow_future</span> <span class="o">=</span> <span class="kc">True</span>
</pre></div>
</div>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">myapp.views</span> <span class="kn">import</span> <span class="n">ArticleTodayArchiveView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;today/&#39;</span><span class="p">,</span>
         <span class="n">ArticleTodayArchiveView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;archive_today&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="admonition-where-is-the-example-template-for-todayarchiveview admonition">
<p class="first admonition-title"><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code> 的示例模板在哪里？</p>
<p class="last">该视图默认使用与前面例子中的 <a class="reference internal" href="flattened-index.html#DayArchiveView" title="DayArchiveView"><code class="xref py py-class docutils literal notranslate"><span class="pre">DayArchiveView</span></code></a> 相同的模板。如果你需要一个不同的模板，请将 <code class="docutils literal notranslate"><span class="pre">template_name</span></code> 属性设置为新模板的名称。</p>
</div>
</dd></dl>

</div>
<div class="section" id="s-datedetailview">
<span id="datedetailview"></span><h2><code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code><a class="headerlink" href="#datedetailview" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.views.generic.dates.DateDetailView">
<em class="property">class </em><code class="descname">DateDetailView</code><a class="headerlink" href="#django.views.generic.dates.DateDetailView" title="永久链接至目标">¶</a></dt>
<dd><p>一个页面代表一个单独的对象。如果对象有一个未来的日期值，除非你把 <code class="docutils literal notranslate"><span class="pre">allow_future</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则视图会默认抛出 404 错误。</p>
<p><strong>祖先（MRO）</strong></p>
<ul class="simple">
<li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectTemplateResponseMixin" title="django.views.generic.detail.SingleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.detail.SingleObjectTemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-simple.html#django.views.generic.base.TemplateResponseMixin" title="django.views.generic.base.TemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.TemplateResponseMixin</span></code></a></li>
<li><a class="reference internal" href="#django.views.generic.dates.BaseDateDetailView" title="django.views.generic.dates.BaseDateDetailView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.BaseDateDetailView</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.YearMixin" title="django.views.generic.dates.YearMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.YearMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.MonthMixin" title="django.views.generic.dates.MonthMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.MonthMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DayMixin" title="django.views.generic.dates.DayMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DayMixin</span></code></a></li>
<li><a class="reference internal" href="mixins-date-based.html#django.views.generic.dates.DateMixin" title="django.views.generic.dates.DateMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.dates.DateMixin</span></code></a></li>
<li><a class="reference internal" href="generic-display.html#django.views.generic.detail.BaseDetailView" title="django.views.generic.detail.BaseDetailView"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.detail.BaseDetailView</span></code></a></li>
<li><a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectMixin" title="django.views.generic.detail.SingleObjectMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.detail.SingleObjectMixin</span></code></a></li>
<li><a class="reference internal" href="base.html#django.views.generic.base.View" title="django.views.generic.base.View"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.views.generic.base.View</span></code></a></li>
</ul>
<p><strong>上下文</strong></p>
<ul class="simple">
<li>包括与 <code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code> 中指定的 <code class="docutils literal notranslate"><span class="pre">model</span></code> 相关的单个对象。</li>
</ul>
<p><strong>注意</strong></p>
<ul class="simple">
<li>使用默认为 <code class="docutils literal notranslate"><span class="pre">_detail</span></code> 的 <code class="docutils literal notranslate"><span class="pre">template_name_suffix</span></code>。</li>
</ul>
<p><strong>示例 myapp/urls.py</strong>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">django.views.generic.dates</span> <span class="kn">import</span> <span class="n">DateDetailView</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&lt;int:year&gt;/&lt;str:month&gt;/&lt;int:day&gt;/&lt;int:pk&gt;/&#39;</span><span class="p">,</span>
         <span class="n">DateDetailView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="n">Article</span><span class="p">,</span> <span class="n">date_field</span><span class="o">=</span><span class="s2">&quot;pub_date&quot;</span><span class="p">),</span>
         <span class="n">name</span><span class="o">=</span><span class="s2">&quot;archive_date_detail&quot;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p><strong>示例 myapp/article_detail.html</strong>：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">object.title</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
</pre></div>
</div>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>上面列出的所有通用视图都有匹配的 <code class="docutils literal notranslate"><span class="pre">Base</span></code> 视图，唯一不同的是它们不包括 <a class="reference internal" href="mixins-multiple-object.html#django.views.generic.list.MultipleObjectTemplateResponseMixin" title="django.views.generic.list.MultipleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultipleObjectTemplateResponseMixin</span></code></a>）用于存档视图）或 <a class="reference internal" href="mixins-single-object.html#django.views.generic.detail.SingleObjectTemplateResponseMixin" title="django.views.generic.detail.SingleObjectTemplateResponseMixin"><code class="xref py py-class docutils literal notranslate"><span class="pre">SingleObjectTemplateResponseMixin</span></code></a> （用于 <a class="reference internal" href="flattened-index.html#DateDetailView" title="DateDetailView"><code class="xref py py-class docutils literal notranslate"><span class="pre">DateDetailView</span></code></a>）。</p>
<dl class="class">
<dt id="django.views.generic.dates.BaseArchiveIndexView">
<em class="property">class </em><code class="descname">BaseArchiveIndexView</code><a class="headerlink" href="#django.views.generic.dates.BaseArchiveIndexView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.views.generic.dates.BaseYearArchiveView">
<em class="property">class </em><code class="descname">BaseYearArchiveView</code><a class="headerlink" href="#django.views.generic.dates.BaseYearArchiveView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.views.generic.dates.BaseMonthArchiveView">
<em class="property">class </em><code class="descname">BaseMonthArchiveView</code><a class="headerlink" href="#django.views.generic.dates.BaseMonthArchiveView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.views.generic.dates.BaseWeekArchiveView">
<em class="property">class </em><code class="descname">BaseWeekArchiveView</code><a class="headerlink" href="#django.views.generic.dates.BaseWeekArchiveView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.views.generic.dates.BaseDayArchiveView">
<em class="property">class </em><code class="descname">BaseDayArchiveView</code><a class="headerlink" href="#django.views.generic.dates.BaseDayArchiveView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="django.views.generic.dates.BaseTodayArchiveView">
<em class="property">class </em><code class="descname">BaseTodayArchiveView</code><a class="headerlink" href="#django.views.generic.dates.BaseTodayArchiveView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<dl class="last class">
<dt id="django.views.generic.dates.BaseDateDetailView">
<em class="property">class </em><code class="descname">BaseDateDetailView</code><a class="headerlink" href="#django.views.generic.dates.BaseDateDetailView" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">通用日期视图</a><ul>
<li><a class="reference internal" href="#archiveindexview"><code class="docutils literal notranslate"><span class="pre">ArchiveIndexView</span></code></a></li>
<li><a class="reference internal" href="#yeararchiveview"><code class="docutils literal notranslate"><span class="pre">YearArchiveView</span></code></a></li>
<li><a class="reference internal" href="#montharchiveview"><code class="docutils literal notranslate"><span class="pre">MonthArchiveView</span></code></a></li>
<li><a class="reference internal" href="#weekarchiveview"><code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code></a></li>
<li><a class="reference internal" href="#dayarchiveview"><code class="docutils literal notranslate"><span class="pre">DayArchiveView</span></code></a></li>
<li><a class="reference internal" href="#todayarchiveview"><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code></a></li>
<li><a class="reference internal" href="#datedetailview"><code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code></a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="generic-editing.html"
                        title="上一章">通用编辑视图</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="mixins.html"
                        title="下一章">基于类的视图混入</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/class-based-views/generic-date-based.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="generic-editing.html" title="通用编辑视图">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="mixins.html" title="基于类的视图混入">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>